package com.sevenstudio.adu.gasing.element;

import java.io.IOException;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;

import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.game.element.GameElementMusic;

public class ASound 
{
	GameEngine theEngine;
	Sound sfx;
	boolean paused;
	
	public ASound(GameEngine mainActivity, GameElementMusic[] arraySfx, int index)
	{
		theEngine = mainActivity;
		paused = false;
		Load(arraySfx,index);
	}
	
	public ASound(GameEngine mainActivity, String sfx)
	{
		theEngine = mainActivity;
		paused = false;
		Load(sfx);
	}
	
	public void Load(GameElementMusic[] arraySfx, int index)
	{
		try {
			sfx = SoundFactory.createSoundFromAsset(theEngine.getSoundManager(), theEngine, arraySfx[index].getPath());
			sfx.setLooping(arraySfx[index].isLooping());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Load(String sfxPath)
	{
		try {
			sfx = SoundFactory.createSoundFromAsset(theEngine.getSoundManager(), theEngine, sfxPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Play()
	{
		if(!GameEngineConfiguration.use_sound)return;
		paused = false;
		sfx.play();
	}
	
	public void Pause()
	{
		if(!GameEngineConfiguration.use_sound)return;
		paused = true;
		sfx.pause();
		
	}
	
	public void Stop()
	{
		if(!GameEngineConfiguration.use_sound)return;
		paused = false;
		sfx.stop();
	}

	public void Resume()
	{
		if(!GameEngineConfiguration.use_sound)return;
		if(paused)
		{
			paused = false;
			sfx.play();
		}
		else
		{
			System.out.println("cannot resume music...");
		}
	}
	
	public Sound theSound() {
		
		return sfx;
	}
	
	public void setVolume(float volume)
	{
		sfx.setVolume(volume);
	}
}

